package fm.dongman.animefm.view.util;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

import org.jetbrains.annotations.NotNull;

/**
 * 分享工具类
 * Created by xuyougen on 2017/11/27.
 */

public final class ShareHelper {
    public static final int TYPE_SHEET = 0x1;   //漫单
    public static final int TYPE_SPECIAL = 0x2; //专题
    public static final int TYPE_COMIC = 0x3;   //动漫

    private static final String mDmFlag = "$dmFM$";     //软件的标记
    private static final String mCode = "cYAqPNdMiV";

    /**
     * 将id转换成code
     *
     * @param id
     * @return
     */
    public static String encode(String id) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < id.length(); i++) {
            int num = Integer.parseInt(String.valueOf(id.charAt(i)));
            result.append(mCode.charAt(num));
        }
        return result.toString();
    }

    /**
     * 将code转换成id
     *
     * @param code
     * @return
     */
    public static String decode(String code) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < code.length(); i++) {
            result.append(mCode.indexOf(code.charAt(i)));
        }
        return result.toString();
    }

    /**
     * 生成本程序特有的分享文本
     *
     * @param title 标题
     * @param id    需要分享对象的id
     * @param type  需要分享对象的类型
     * @return
     */
    public static String generateShareText(String title, String id, int type) {
        if (type == TYPE_SHEET) {
            return "分享漫单 \"" + title + "\" ，复制此消息 $dmFM$[s]***" + encode(id) + "***，打开客户端即可查看，客户端下载 http://m.dongman.fm";
        } else if (type == TYPE_SPECIAL) {
            return "分享专题 \"" + title + "\" ，复制此消息 $dmFM$[sp]***" + encode(id) + "***，打开客户端即可查看，客户端下载 http://m.dongman.fm";
        } else if (type == TYPE_COMIC) {
            return "向你分享非常好看的动漫 《" + title + "》 ，复制此消息 $dmFM$[c]***" + encode(id) + "***，打开客户端即可查看，客户端下载 http://m.dongman.fm";
        }
        return null;
    }

    /**
     * 检查剪切板是否动漫FM的分享数据
     *
     * @param context
     * @return
     */

    public static void checkClipBoardIsExistShare(Context context, @NotNull OnFoundShareInfoListener listener) {
        ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        //没有数据直接return
        if (manager == null || manager.getPrimaryClip() == null)
            return;
        String data = manager.getPrimaryClip().getItemAt(0).getText().toString();
        if (data.contains(mDmFlag)) {
            manager.setPrimaryClip(ClipData.newPlainText("dmFM", ""));
            //将id取出来并解码
            String id = decode(data.substring(data.indexOf("***") + 3, data.lastIndexOf("***")));
            if (data.contains("[s]")) {
                listener.onFound(TYPE_SHEET, id);
            } else if (data.contains("[sp]")) {
                listener.onFound(TYPE_SPECIAL, id);
            } else if (data.contains("[c]")) {
                listener.onFound(TYPE_COMIC, id);
            }
        }
    }

    public interface OnFoundShareInfoListener {
        void onFound(int type, String id);
    }
}
